/**
 * Created on 24.07.2012
 */
package com.example.utils;

import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.example.ejb.UsersConnection;
import com.example.ejb.exceptions.DatabaseException;
import com.example.ejb.exceptions.UserAlreadyExistsException;
import com.example.ejb.exceptions.UserDoesNotExistException;
import com.example.ejb.model.DbUser;

/**
 * @author Ciprian Mihalache
 *
 */
public final class UsersDelegate {
	private UsersConnection usersConnection;

	public UsersDelegate() {
		init();
	}

	private void init() {
		try {
			InitialContext ctx = new InitialContext();
			usersConnection = (UsersConnection) ctx.lookup(UsersConnection.JNDI_NAME);
		} catch (NamingException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}

	public List<DbUser> getUsers() {
		return usersConnection.getAllUsers();
	}

	public DbUser getUserByDbId(long userDbId) throws DatabaseException {
		return usersConnection.getUserByDbId(userDbId);
	}

	public final void addUser(DbUser u) throws UserAlreadyExistsException,
			DatabaseException {
		usersConnection.addUser(u);
	}

	public void updateUser(DbUser other) throws UserDoesNotExistException,
			DatabaseException {
		usersConnection.updateUser(other);
	}

	public void deleteUser(long id) throws UserDoesNotExistException, DatabaseException {
		usersConnection.deleteUser(id);
	}

}
